package com.jinglu.cloud.common.model.vo;

import com.jinglu.cloud.common.utils.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @Author lvzhenzhao
 * @Datetime 2021/03/02-下午2:27
 */
@Data
@NoArgsConstructor
public class PageVO<T> implements Serializable {

    /**
     * 当前页码
     */
    private Long current;

    /**
     * 返回条数
     */
    private Long size;

    /**
     * 总条数
     */
    private Long total;

    /**
     * 总页数
     */
    private Long pageCount;

    /**
     * 实体列表
     */
    private List<T> dataList;

    public PageVO(IPage<T> data) {
        if (data instanceof IPage) {
            this.total = data.getTotal();
            this.size = data.getSize();
            this.pageCount = data.getPages();
            this.current = data.getCurrent();
            this.dataList = data.getRecords();
        }
    }

    /**
     * 构建分页VO
     *
     * @param data  分页列表
     * @param clazz 目标类
     * @return
     * @date 2022/7/12 10:42
     */
    public PageVO(IPage data, Class<T> clazz) {
        this.total = data.getTotal();
        this.size = data.getSize();
        this.pageCount = data.getPages();
        this.current = data.getCurrent();
        this.dataList = BeanUtils.convertList(data.getRecords(), clazz);
    }

}
